package com.aerozhonghuan.logisticsmall.framework.hybrid.actions;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Toast;

import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.hybrid.NewWebviewFragment;
import com.aerozhonghuan.logisticsmall.framework.hybrid.SimpleActionHandler;
import com.aerozhonghuan.logisticsmall.framework.hybrid.bean.JsMobileAgentConstants;
import com.aerozhonghuan.foundation.log.LogUtil;
import com.aerozhonghuan.foundation.umeng.ShareHelper;
import com.aerozhonghuan.hybrid.XJsCallback;
import com.umeng.socialize.bean.SHARE_MEDIA;

import org.json.JSONObject;

/**
 * 描述
 * 作者 Zhang Yonghui
 * 创建日期 2019/1/21
 */
public class WeChatShareActionHandler extends SimpleActionHandler {
    private static final String TAG = "WeChatShareActionHandle";
    /**
     * 未指定，此时会先让弹出分享方式选择框让用户选择
     */
    private static final int SHARETYPE_UNSELECTED = 0;
    /**
     * 好友
     */
    private static final int SHARETYPE_SESSION = 1;
    /**
     * 朋友圈
     */
    private static final int SHARETYPE_TIME_LINE = 2;

    ShareHelper.CustomShareListener2 customShareListener2 = new ShareHelper.CustomShareListener2() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            super.onStart(platform);
            LogUtil.d(TAG, "ShareHelper callback onStart");
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            LogUtil.d(TAG, "ShareHelper callback onResult " + platform);
            Toast.makeText(getActivity(), "分享成功", Toast.LENGTH_SHORT).show();
        }

        // 微信线程,非主线程
        @Override
        public void onError(SHARE_MEDIA platform, final Throwable t) {
            LogUtil.d(TAG, "ShareHelper callback onError " + platform + " err=" + t);
            if (SHARE_MEDIA.WEIXIN.equals(platform) || SHARE_MEDIA.WEIXIN_CIRCLE.equals(platform) || SHARE_MEDIA.WEIXIN_FAVORITE.equals(platform)) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!TextUtils.isEmpty(t.getMessage()) && t.getMessage().contains("没有安装应用")) {
                            Toast.makeText(getActivity(), "未安装微信，无法分享", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getActivity(), "分享失败," + t.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            super.onCancel(platform);
            LogUtil.d(TAG, "ShareHelper callback onCancel");
        }
    };

    public WeChatShareActionHandler(NewWebviewFragment fragment) {
        super(JsMobileAgentConstants.FLAG_WECHATSHARE, fragment);
    }

    @Override
    public void handleAction(@NonNull String action, @Nullable JSONObject params, @Nullable XJsCallback callBack) {
        if (params == null) {
            return;
        }
        String title = params.optString("title");
        String content = params.optString("content");
        String imagePath = params.optString("imagePath");
        String url = params.optString("url");
        if (params.has("shareType") && params.optInt("shareType") != SHARETYPE_UNSELECTED) {
            new ShareHelper(getActivity(), title, content, url, R.mipmap.ic_launcher, params.optInt("shareType"), customShareListener2).open();
        } else {
            new ShareHelper(getActivity(), title, content, url, R.mipmap.ic_launcher, customShareListener2).open();
        }
    }
}
